import yaml as pyyaml

from django.utils import simplejson
from google.appengine.ext import db

import core
import render


class Plugin(core.ContentType):
  """A YAML Content.

  These form the basis for datatypes and can embed references to
  any of the other Content types.
  """

  @classmethod
  def Customize(cls, content, src, **kwargs):
    content.src = src

  @classmethod
  def Html(cls, content, editor=False, **kwargs):
    template_dir = 'content_types'
    template_name = cls.name + '.html'

    # TODO: Transform using pygments
    if content and content.src:
      kwargs['content_html'] = content.src

    return render.Render(
        template_dir, template_name, content_type=cls, content=content,
        editor=editor, **kwargs)

  @classmethod
  def Json(cls, content):
    entries = list(pyyaml.safe_load_all(content.src))
    if len(entries) > 1 and type(entries[0]) is str:
      data = {entries[0]: entries[1:]}
    else:
      data = entries
    return simplejson.dumps(data)
